package com.travel.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.travel.entity.Scenic;
import com.travel.entity.ScenicComment;
import com.travel.service.ScenicCommentService;
import com.travel.service.impl.ScenicCommentServiceImpl;
import com.travel.util.PageInfo;

/**
 * Servlet implementation class ScenicCommentServlet
 * @author zourenhe
 */
@WebServlet("/ScenicCommentServlet")
public class ScenicCommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ScenicCommentService scenicCommentService = new ScenicCommentServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScenicCommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if (op==null) {
			// 查询列表
			doFindScenicComment(request, response);
		} else if("delete".equals(op)){
			doDeleteHotelComment(request, response);
		} else if("searchByPage".equals(op)) {
			// 分页查询列表
			doQueryCommentByPage(request, response);
		} else if ("publishComment".equals(op)) {
			//发表评论
			doPublishComment(request,response);
		}
	}

	private void doPublishComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取评论内容,评论时间,用户编号,景点编号
		String commentContent = request.getParameter("commentContent");
		String commentDatetime = request.getParameter("commentDatetime");
		String userId = request.getParameter("userId");
		String scenicId = request.getParameter("scenicId");
		
		//调用新增景点评论的方法
		ScenicComment scenicComment = new ScenicComment(null, commentContent, commentDatetime, Integer.parseInt(userId), Integer.parseInt(scenicId), null, null, null);
		scenicCommentService.insert(scenicComment);
		
		//再次查询评论列表
		doFindScenicComment(request,response);
	}

	private void doQueryCommentByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		PageInfo<ScenicComment> pageInfo=scenicCommentService.getUserListByPage(pageNum, pageSize);
		ObjectMapper  objectMapper = new ObjectMapper();
		String  json = objectMapper.writeValueAsString(pageInfo);		
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doDeleteHotelComment(HttpServletRequest request, HttpServletResponse response) throws IOException {		
		String  commentId = request.getParameter("commentId");
		scenicCommentService.deleteById(Integer.parseInt(commentId));
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));	
		PageInfo<ScenicComment> pageInfo=scenicCommentService.getUserListByPage(pageNum, pageSize);
		ObjectMapper  objectMapper = new ObjectMapper();
		String  json = objectMapper.writeValueAsString(pageInfo);		
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(json);		
	}
	private void doFindScenicComment(HttpServletRequest request, HttpServletResponse response) throws IOException {

		List<ScenicComment> list = scenicCommentService.getScenicCommentList();
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter  out= response.getWriter();
		out.print(json);
		
	}

	

}
